<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Goods;
use App\Services\Traits\UploadImageTrait;

class ImageController extends Controller
{

    use UploadImageTrait;

    protected $path = 'upload/images/';

    protected $check_field = 'goodsImage';
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Goods $goods)
    {
        return view('admins.merchant.images');
    }

    public function all(Request $request, Goods $goods)
    {
        $result = $goods->filter($request->all())
            ->where('goods.state', 1)
            ->select('id','goods_image','domainName')
            ->orderBy('goods.created_at', 'desc')
            ->paginate(config('api.page.limit'));

        return response()->json(['code' => 200, 'msg' => 'success', 'data' => $result]);
    }

    public function update(Request $request, Goods $goods)
    {
        $goods = $goods->find($request->goods_id);
        $goods->goods_image = $request->goods_image;
        $goods->domainName = $request->domainName;
        $res = $goods->save();
        if($res){
            return response()->json(['code' => 200, 'msg' => 'success']);
        }else{
            return response()->json(['code' => 400, 'msg' => 'error']);
        }
    }


}
